#include <iostream>
#include <string.h>
#include "ShareMemory.hpp"
#include "Time.hpp"
#include "Fifo.hpp"

int main()
{
    shm.GetShm();
    shm.AttachShm();

    // 1.数据不一致: 刚写完一个字符, 立刻就读走了
    // char* strinfo = (char*)shm.GetAddr();
    // std::cout << "client addr: " << (void*)strinfo << std::endl;
    // char ch = 'A';
    // while(ch <= 'Z')
    // {
    //     sleep(1);
    //     strinfo[ch - 'A'] = ch;
    //     ch++;
    // }

    // 2.添加两个命名管道, 模拟进程间同步, 保证数据一致性
    fifoA.OpenFifoForWrite();
    fifoB.OpenFifoForRead();
    struct data *data = (struct data *)shm.GetAddr();
    char ch = 'A';
    while (ch <= 'Z')
    {
        strcpy(data->status, "最新");
        strcpy(data->lasttime, GetCurTime().c_str());
        strcpy(data->image, "xxxxxxxxxxxxxxxxxxxxx");

        fifoA.FifoWrite();
        fifoB.FifoRead(); // 当命名管道正常 && 写端为空时: 读端阻塞

        sleep(3);
    }

    shm.DetachShm();

    return 0;
}